package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {

    /**
     * 根据名称分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findByNameAndPage(ChannelDto dto) {
        //判断是否携带参数
        if (null == dto) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        dto.checkParam();

        //定义分页对象
        Page<AdChannel> channelPage = new Page<>(dto.getPage(), dto.getSize());

        //定义查询对象
        LambdaQueryWrapper<AdChannel> wrapper = Wrappers.lambdaQuery();
        if (null != dto.getName()) {
            wrapper.like(AdChannel::getName, dto.getName());
        }

        //查询数据
        IPage<AdChannel> page = page(channelPage, wrapper);

        //响应对象
        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());

        return result;
    }

    /**
     * 新增
     *
     * @param channel
     * @return
     */
    @Override
    public ResponseResult insertChannel(AdChannel channel) {
        //判断参数是否为空
        if (null == channel) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //补充创建时间
        channel.setCreatedTime(new Date());

        //存入数据
        boolean save = save(channel);

        //判断是否保存成功
        if (save) {
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.DATA_EXIST);
    }

    /**
     * 修改频道
     *
     * @param channel
     * @return
     */
    @Override
    public ResponseResult updateChannel(AdChannel channel) {
        //判断参数是否为空
        if (null == channel && null == channel.getId()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //修改数据
        boolean updateById = updateById(channel);

        //判断是否修改成功
        if (updateById) {
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.DATA_NOT_EXIST);
    }


    /**
     * 删除频道
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteChannel(Integer id) {
        //判断参数是否为空
        if (null == id && id <= 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //获取当前频道数据
        AdChannel byId = getById(id);

        //判断是否为空
        if (null == byId) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        //判断频道是否可用
        if (byId.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }

        //删除数据
        boolean removeById = removeById(id);

        //判断是否删除成功
        if (removeById) {
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.DATA_NOT_EXIST);
    }
}
